/***********************************************************************
 * Module:  GraphDocumentView.java
 * Author:  Nikola
 * Purpose: Defines the Class GraphDocumentView
 ***********************************************************************/

package genedit.view.graph;

import genedit.model.graph.GraphDocument;
import genedit.model.graph.GraphElement;

import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.util.List;
import java.util.Observable;

public class GraphDocumentView extends genedit.view.DocumentView {
	private List<ElementPainter> elementPainters;

	/** @param model */
	public GraphDocumentView(GraphDocument model) {
		super(model);
		workArea = new GraphWorkArea(this);
	}

	/** @param g */
	public void paint(Graphics2D g) {
		// TODO: implement
	}

	/**
	 * @param o
	 * @param arg
	 */
	public void update(Observable o, Object arg) {
		int n = getElementPainters().size();

		for (int i = n - 1; i > -1; i--) {
			ElementPainter painter = elementPainters.get(i);
			if (!model.getElements().contains(painter.getElement()))
				removeElementPainters(painter);
		}

		workArea.repaint();
	}

	/** @pdGenerated default getter */
	public List<ElementPainter> getElementPainters() {
		if (elementPainters == null)
			elementPainters = new java.util.ArrayList<ElementPainter>();
		return elementPainters;
	}

	/** @pdGenerated default iterator getter */
	public java.util.Iterator<ElementPainter> getIteratorElementPainters() {
		if (elementPainters == null)
			elementPainters = new java.util.ArrayList<ElementPainter>();
		return elementPainters.iterator();
	}

	/**
	 * @pdGenerated default setter
	 * @param newElementPainters
	 */
	public void setElementPainters(java.util.Collection<ElementPainter> newElementPainters) {
		removeAllElementPainters();
		for (java.util.Iterator<ElementPainter> iter = newElementPainters.iterator(); iter
				.hasNext();)
			addElementPainters((ElementPainter) iter.next());
	}

	/**
	 * @pdGenerated default add
	 * @param newElementPainter
	 */
	public void addElementPainters(ElementPainter newElementPainter) {
		if (newElementPainter == null)
			return;
		if (this.elementPainters == null)
			this.elementPainters = new java.util.ArrayList<ElementPainter>();
		if (!this.elementPainters.contains(newElementPainter))
			this.elementPainters.add(newElementPainter);
	}

	/**
	 * @pdGenerated default remove
	 * @param oldElementPainter
	 */
	public void removeElementPainters(ElementPainter oldElementPainter) {
		if (oldElementPainter == null)
			return;
		if (this.elementPainters != null)
			if (this.elementPainters.contains(oldElementPainter))
				this.elementPainters.remove(oldElementPainter);
	}

	/** @pdGenerated default removeAll */
	public void removeAllElementPainters() {
		if (elementPainters != null)
			elementPainters.clear();
	}

	public ElementPainter getElementAtPosition(Point2D position) {
		for (ElementPainter painter : getElementPainters()) {
			if (painter.isElementAt(position))
				return painter;
		}
		return null;
	}

	public ElementPainter getPainter(GraphElement element){
		for (ElementPainter el : getElementPainters()) {
			if (el.getElement().equals(element))
				return el;
		}
		return null;
	}
	
}